<?php

namespace App\Http\Controllers\Admin;

use App\Facades\EasyWechat;
use App\Services\WxQrcode\WxQrcodeGroupService;
use App\Services\WxQrcode\WxQrcodeService;
use Illuminate\Http\Request;
/**
 * 微信二维码管理
 * @package App\Http\Controllers\Admin
 */
class WxQrcodeController extends BaseWxController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 二维码列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request, WxQrcodeGroupService $groupSer)
    {
        //当前选择微信应用数据
        $curWxMp = \Auth::user()->getWxMpCurrent();
        $appid = $curWxMp ? $curWxMp['appid'] : '';
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxQrcodeUse::select('*');
            //当前选中二维码
            $query->where('appid', $appid);
            //关键字
            $keyword = $request->get('keyword', '');
            if ($keyword){
                $query->where('name', 'like', '%'.$keyword.'%');
            }
            //分组
            $groupId = $request->get('group_id', 0);
            if ($groupId){
                $query->where('group_id', $groupId);
            }
            $list = $query->paginate($pageSize);
            if ($list){
                $app = EasyWechat::officialAccount($appid);
                foreach ($list as $k => $v){
                    $v['group'] = $v->group;
                    $v['qrcode'] = $v->qrcode;
                    $v['qrcode_url'] = $app->qrcode->url($v['qrcode']['ticket']);
                    $list[$k] = $v;
                }
            }
            return $list;
        }
        //二维码分组数据
        $groupList = $groupSer->getGroupList();
        $this->viewData['groupList'] = $groupList;
        return view('admin.wxqrcode.index', $this->viewData);
    }
    /**
     * 新建分组
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
     */
    public function save(\App\Http\Requests\AdminWxQrcodePost $request, WxQrcodeService $qrcodeSer)
    {
        try {
            $qrcodeData = $request->toArray();
            if(!$qrcodeSer->saveQrcodeUse($qrcodeData)){
                throw new \Exception('保存失败');
            }
            return $this->showMsg('success', '新增成功', [], 'admin.sysmenugroup.index');
        }catch (\Exception $e){
            return $this->showMsg('error', $e->getMessage());
        }
    }
    /**
     * 删除
     * @param Request $request
     */
    public function destroy(Request $request, WxQrcodeService $qrcodeSer)
    {
        try {
            $qrcodeId = $request->input('qrcode_id', 0);
            $qrcodeSer->releaseQrcodeUse($qrcodeId);
            return $this->showMsg('success', '删除成功', [], 'admin.sysmenugroup.index');
        }catch (\Exception $e){
            return $this->showMsg('error', '删除失败:'.$e->getMessage());
        }
    }

    /**
     * 扫码日志
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function scanlog(Request $request, WxQrcodeGroupService $groupSer)
    {
        //当前选择微信应用数据
        $curWxMp = \Auth::user()->getWxMpCurrent();
        $appid = $curWxMp ? $curWxMp['appid'] : '';
        if ($request->ajax()){
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\WxQrcodeScanlog::select('*');
            //当前选中二维码
            $query->where('appid', $appid);
            //扫码用户openid
            $from_openid = $request->get('from_openid', '');
            if ($from_openid){
                $query->where('from_openid', $from_openid);
            }
            //二维码ID
            $qrcode_id = $request->get('qrcode_id', 0);
            if ($qrcode_id){
                $query->where('qrcode_id', $qrcode_id);
            }
            //关联业务ID
            $relation_id = $request->get('relation_id', '');
            if ($relation_id){
                $query->where('relation_id', $relation_id);
            }
            //分组
            $groupId = $request->get('group_id', 0);
            if ($groupId){
                $query->where('group_id', $groupId);
            }
            $list = $query->paginate($pageSize);
            if ($list){
                $app = EasyWechat::officialAccount($appid);
                foreach ($list as $k => $v){
                    $v['group'] = $v->group;
                    $v['qrcode'] = $v->qrcode;
                    $v['qrcode_url'] = $app->qrcode->url($v['qrcode']['ticket']);
                    $list[$k] = $v;
                }
            }
            return $list;
        }
        //二维码分组数据
        $groupList = $groupSer->getGroupList();
        $this->viewData['groupList'] = $groupList;
        return view('admin.wxqrcode.scanlog', $this->viewData);
    }
}
